package com.lnking.lnkingplace.controller;


import com.lnking.common.http.ReturnResult;
import com.lnking.lnkingplace.Util.UserUtil;
import com.lnking.lnkingplace.mapper.IsolationWorkerMapper;
import com.lnking.lnkingplace.model.dto.isolation.worker.WorkerExcelDto;
import com.lnking.lnkingplace.model.dto.isolation.worker.WorkerListDto;
import com.lnking.lnkingplace.model.enums.UserType;
import com.lnking.lnkingplace.model.request.isolation.worker.WorkerSearchInfoRequest;
import com.lnking.lnkingplace.model.request.isolation.worker.WorkerSearchRequest;
import com.lnking.lnkingplace.model.request.isolation.worker.WorkerUpdateRequest;
import com.lnking.lnkingplace.service.IIsolationWorkerService;
import com.lnking.starter.export.utils.ExportUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zhangqt
 * @since 2022-08-27
 */
@RestController
@RequestMapping("/isolation-worker")
@Api(tags = "隔离场所工作人员")
public class IsolationWorkerController {

    @Resource
    IIsolationWorkerService iIsolationWorkerService;
    @Resource
    UserUtil userUtil;

    @Resource
    IsolationWorkerMapper workerMapper;

    @ApiOperation(value = "导出")
    @GetMapping("export")
    public void export(WorkerSearchRequest params, HttpServletResponse response) throws Exception {
        List<WorkerListDto> data = (List<WorkerListDto>)iIsolationWorkerService.pageList(params);
        ExportUtils.export(data, response, WorkerExcelDto.class);
    }

    @ApiOperation("导入")
    @PostMapping(value = "/import")
    public ReturnResult importExcel(String placeId,
                                   @RequestParam("file") MultipartFile file) throws Exception{
        iIsolationWorkerService.importExcel(placeId,file);
        return ReturnResult.success();
    }

    @ApiOperation("扫码用")
    @GetMapping("info-list")
    public ReturnResult getInfoList(WorkerSearchInfoRequest params) {
        return ReturnResult.success(workerMapper.queryInfoList(params));
    }

    @ApiOperation("查询列表")
    @GetMapping("list")
    public ReturnResult getList(WorkerSearchRequest params) {
        if(userUtil.getUser().getType().equals(UserType.PLACE)){
            params.setPlaceName(userUtil.getUser().getName());
        }
        return ReturnResult.success(iIsolationWorkerService.pageList(params));
    }

    @ApiOperation("一键下线")
    @GetMapping("update-offline")
    public ReturnResult updateOffline(){
        iIsolationWorkerService.updateOffline();
        return ReturnResult.success();
    }

    @PostMapping("update")
    @ApiOperation("修改")
    public ReturnResult update(@RequestBody WorkerUpdateRequest params) throws Exception{
        iIsolationWorkerService.update(params);
        return ReturnResult.success();
    }

    @ApiOperation("删除")
    @DeleteMapping("/{id}")
    public ReturnResult delete(@PathVariable("id") String id) {
        iIsolationWorkerService.removeById(id);
        return ReturnResult.success();
    }

    @ApiOperation("查询详情")
    @GetMapping("get-details")
    public ReturnResult getDetails(String id){
        return ReturnResult.success(iIsolationWorkerService.getById(id));
    }
}
